package com.ljkj.cordial.chat.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanjiu.lib.imclient.dto.SettingsDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.FontSize;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.SysSettingView;
import com.ljkj.cordial.chat.util.ReadFinishCycTimeUtil;
import com.ljkj.cordial.chat.util.SharedUserUtils;
import com.ljkj.cordial.chat.widget.SingleCommonDialog;
import com.nic.view.util.JToast;
import com.nic.view.util.SharedUtils;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 系统设置
 */
public class SysSettingActivity extends AppEventActivity<SysSettingView> {


    @BindView(R.id.tv_cyc_clear_prompt)
    TextView tvCycClearPrompt;
    @BindView(R.id.tv_cyc_clear)
    TextView tvCycClear;
    @BindView(R.id.ll_cyc_clear)
    LinearLayout llCycClear;
    @BindView(R.id.tv_read_finish_clear)
    TextView tvReadFinishClear;
    @BindView(R.id.ll_read_finish_clear)
    LinearLayout llReadFinishClear;
    @BindView(R.id.sw_offline_msg)
    SwitchCompat swOfflineMsg;
    @BindView(R.id.sw_everyday_clear_prompt)
    TextView swEverydayClearPrompt;
    @BindView(R.id.sw_everyday_clear)
    SwitchCompat swEverydayClear;
    @BindView(R.id.sw_read_finish_clear)
    SwitchCompat swReadFinishClear;
    @BindView(R.id.ll_version)
    LinearLayout llVersion;
    @BindView(R.id.sw_read_finish_clear1)
    SwitchCompat swReadFinishClear1;
    @BindView(R.id.tv_read_finish_clear1)
    TextView tvReadFinishClear1;
    @BindView(R.id.ll_read_finish_clear1)
    LinearLayout llReadFinishClear1;
    @BindView(R.id.sw_message_tone)
    SwitchCompat swMessageTone;
    @BindView(R.id.cb_message_tone)
    CheckBox cbMessageTone;
    @BindView(R.id.ll_text)
    LinearLayout llText;
    @BindView(R.id.tv_text_size)
    TextView tvTextSize;
    @BindView(R.id.cb_everyday_clear)
    CheckBox cbEverydayClear;
    @BindView(R.id.cb_log)
    CheckBox cbLog;

    private int sysClearCycPosition;


    @Override
    public int getContentLayoutId() {
        return R.layout.activity_sys_setting;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setTitle("系统设置");
        boolean r = (boolean) SharedUtils.get(this, Canstants.KEY_SYS_IS_RECEIVE_OFFLINE_MSG, true);
        boolean e = (boolean) SharedUserUtils.get(this, Canstants.getClearKey(), false);
        boolean mt = (boolean) SharedUserUtils.get(this, Canstants.KEY_SYS_IS_MESSAGE_TONE, true);
        swOfflineMsg.setChecked(r);
//        swEverydayClear.setChecked(e);
        cbEverydayClear.setChecked(e);
//        swMessageTone.setChecked(mt);
        cbMessageTone.setChecked(mt);
        swOfflineMsg.setOnClickListener(v -> {
            if (null != obtainView())
                obtainView().doSetOfflineMsg(swOfflineMsg.isChecked());
        });

//        swEverydayClear.setOnClickListener(v -> {
//            if (UserPreferences.isRegistered()) {
//                if (null != obtainView())
//                    obtainView().doSetExitClean(swEverydayClear.isChecked());
//            }
//        });
        cbEverydayClear.setOnClickListener(v -> {
            if (UserPreferences.isRegistered()) {
                if (null != obtainView())
                    obtainView().doSetExitClean(cbEverydayClear.isChecked());
            }
        });
        sysClearCycPosition = (int) SharedUserUtils.get(this, Canstants.getSysClearCycKey(), 0);
        tvCycClear.setText(getCycClear(R.array.array_cyc_clear)[sysClearCycPosition]);
        ReadFinishCycTimeUtil.getInstance().setReadFinishCycTime(this, Canstants.getSingleReadFinishKey(ChatApp.getInstance().getUserId()), tvReadFinishClear);
        ReadFinishCycTimeUtil.getInstance().setReadFinishCycChangeListener(this, Canstants.isSingleReadFinishKey(ChatApp.getInstance().getUserId()), swReadFinishClear, llReadFinishClear);
        ReadFinishCycTimeUtil.getInstance().setReadFinishCycTime(this, Canstants.getGroupReadFinishKey(ChatApp.getInstance().getUserId()), tvReadFinishClear1);
        ReadFinishCycTimeUtil.getInstance().setReadFinishCycChangeListener(this, Canstants.isGroupReadFinishKey(ChatApp.getInstance().getUserId()), swReadFinishClear1, llReadFinishClear1);
//        cpOfflineMsg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    cpOfflineMsg.setCloseIconVisible(false);
//                    cpOfflineMsg.setText("OFF");
//                    cpOfflineMsg.setChipBackgroundColorResource(R.color.item_blue);
//                } else {
//                    cpOfflineMsg.setCloseIconVisible(true);
//                    cpOfflineMsg.setText(" ON ");
//                    cpOfflineMsg.setChipBackgroundColorResource(R.color.gray_500);
//                }
//            }
//        });
//        cpOfflineMsg.setOnCloseIconClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                cpOfflineMsg.setChecked(true);
//            }
//        });
        if (!UserPreferences.isRegistered()) {
            swEverydayClearPrompt.setTextColor(ContextCompat.getColor(this, R.color.gray_600));
            tvCycClearPrompt.setTextColor(ContextCompat.getColor(this, R.color.gray_600));
        }
//        swMessageTone.setOnCheckedChangeListener((buttonView, isChecked) -> SharedUserUtils.put(this, Canstants.KEY_SYS_IS_MESSAGE_TONE, isChecked));
        cbMessageTone.setOnCheckedChangeListener((buttonView, isChecked) -> SharedUserUtils.put(this, Canstants.KEY_SYS_IS_MESSAGE_TONE, isChecked));
        int position = FontSize.getFontSizePosition(ChatApp.getInstance().getFontSize());
        tvTextSize.setText(FontSize.getFontSizeText(position));
        cbLog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {

                } else {

                }
            }
        });
    }

    @Override
    protected boolean afterOnStartRegisterEventBus() {
//        obtainView().doGetSettingsInfo();
        return super.afterOnStartRegisterEventBus();
    }

    private int mTimerCleanPos;
    private String mTimerCleanText;

    @OnClick({R.id.ll_text, R.id.ll_cyc_clear, R.id.ll_read_finish_clear, R.id.ll_version, R.id.ll_read_finish_clear1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_text:
                int position = FontSize.getFontSizePosition(ChatApp.getInstance().getFontSize());
                tvTextSize.setText(FontSize.getFontSizeText(position));
                new SingleCommonDialog(this)
                        .setTitle("字体大小")
                        .setSelected(position)
                        .setFontSizeData()
                        .setLeftSubmit("取消", null).setRightSubmitGray("确认", new SingleCommonDialog.OnSingleItemClickListener() {
                    @Override
                    public void onClick(View view, int position, String value) {
                        tvTextSize.setText(FontSize.getFontSizeText(position));
                        if (position == 0) {
                            ChatApp.getInstance().setFontSize(FontSize.FONT_NORMAL);
                        } else if (position == 1) {
//                            ChatApp.getInstance().setFontSize(FontSize.FONT_MAX);
//                        } else if (position == 2) {
                            ChatApp.getInstance().setFontSize(FontSize.FONT_OVER_MAX);
                        } else if (position == 2) {
                            ChatApp.getInstance().setFontSize(FontSize.FONT_OVER_MAX2);
                        }

                        startActivity(new Intent(ChatApp.getContext(), SplashActivity.class));
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                        ChatApp.getInstance().quitApp();
                    }
                }).show();
                break;
            case R.id.ll_cyc_clear:
                sysClearCycPosition = (int) SharedUserUtils.get(this, Canstants.getSysClearCycKey(), 0);
                if (UserPreferences.isRegistered()) {
                    new SingleCommonDialog(this)
                            .setTitle("清理周期选择")
                            .setSelected(sysClearCycPosition)
                            .setClearData()
                            .setLeftSubmit("取消", null).setRightSubmitGray("确认", new SingleCommonDialog.OnSingleItemClickListener() {
                        @Override
                        public void onClick(View view, int position, String value) {
                            mTimerCleanPos = position;
                            mTimerCleanText = value;
                            if (null != obtainView())
                                obtainView().doSetTimerClean(mTimerCleanPos);
                        }
                    }).show();
                }
                break;
            case R.id.ll_read_finish_clear://好友阅后即焚设置
                ReadFinishCycTimeUtil.getInstance().showPop(this, Canstants.getSingleReadFinishKey(ChatApp.getInstance().getUserId()), tvReadFinishClear);
                break;
            case R.id.ll_read_finish_clear1://群聊阅后即焚设置
                ReadFinishCycTimeUtil.getInstance().showPop(this, Canstants.getGroupReadFinishKey(ChatApp.getInstance().getUserId()), tvReadFinishClear1);
                break;
            case R.id.ll_version:
                openActivity(VersionInfoActivity.class);
                break;
        }
    }


    private String[] getCycClear(int p) {
        return getResources().getStringArray(p);
    }


    @Override
    protected SysSettingView bindNettyMessageView() {
        return new SysSettingView(new SysSettingView.SysSettingCallback() {

            @Override
            public void onSettingsInfoCallback(int code, String msg, SettingsDto data) {
                if (code == IMSContacts.ResponseCode.SUCCESS && data != null) {
                    SharedUserUtils.put(mContext, Canstants.getClearKey(), data.isExitClean());
                    cbEverydayClear.setChecked(data.isExitClean());
                    int timerClean = data.getTimerClean() - 1;
                    if (timerClean >= 0 && timerClean <= 2) {
                        tvCycClear.setText(getCycClear(R.array.array_cyc_clear)[timerClean]);
                        SharedUserUtils.put(mContext, Canstants.getSysClearCycKey(), timerClean);
                        SharedUserUtils.put(mContext, Canstants.getSysClearCycTimeKey(), Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
                    }
                } else {
                    JToast.showShort(SysSettingActivity.this, data == null ? "设置信息获取失败" : msg);
                }
            }

            @Override
            public void onSetOfflineMsgCallback(int code, String msg) {
                if (code == IMSContacts.ResponseCode.SUCCESS) {
                    SharedUtils.put(mContext, Canstants.KEY_SYS_IS_RECEIVE_OFFLINE_MSG, swOfflineMsg.isChecked());
                } else {
                    JToast.showShort(SysSettingActivity.this, "设置失败");
                    swOfflineMsg.setChecked(!swOfflineMsg.isChecked());
                }
            }

            @Override
            public void onSetExitCleanCallback(int code, String msg) {
                if (code == IMSContacts.ResponseCode.SUCCESS) {
                    SharedUserUtils.put(mContext, Canstants.getClearKey(), cbEverydayClear.isChecked());
                } else {
                    cbEverydayClear.setChecked(!cbEverydayClear.isChecked());
                    JToast.showShort(SysSettingActivity.this, msg);
                }
            }

            @Override
            public void onSetTimerCleanCallback(int code, String msg) {
                if (code == IMSContacts.ResponseCode.SUCCESS) {
                    tvCycClear.setText(mTimerCleanText);
                    SharedUserUtils.put(mContext, Canstants.getSysClearCycKey(), mTimerCleanPos);
                    SharedUserUtils.put(mContext, Canstants.getSysClearCycTimeKey(), Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
                } else {
                    JToast.showShort(SysSettingActivity.this, msg);
                }
            }
        });
    }
}
